//
//  BHCircleGuideChoseView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/6.
//

import UIKit
protocol BHCircleGuideChoseViewDelegate {
    func didGuideViewSelectIndexType(index: Int, columnIds: [Int:String])
}

class BHCircleGuideChoseView: UIView {
    var delegate: BHCircleGuideChoseViewDelegate?
    

    @IBOutlet weak var skipBtn: UIButton!
    
    @IBOutlet weak var doneBtn: UIButton!
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    fileprivate var columnIdChoseDic: [Int:String] = [:]
    fileprivate var indexSource: [Int:Int] = [:]
    
    var columnArray: [BHCircleColumnModel] = [] {
        didSet {
            self.collectionView.reloadData()
        }
    }
    
    
    static func loadFromXib() -> BHCircleGuideChoseView {
        let viewArray = Bundle.main.loadNibNamed("BHCircleGuideChoseView", owner: nil, options: nil)
        let view: BHCircleGuideChoseView = viewArray![0] as! BHCircleGuideChoseView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.skipBtn.addLayerCorner(12, 0.5, .color_999999)
        self.doneBtn.addLayerCorner(22, 0, .color_999999)

        self.collectionView.regiseterNib(nibName: "BHCircleTextItemCollectionViewCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
    }
    @IBAction func skipAct(_ sender: UIButton) {
        self.delegate?.didGuideViewSelectIndexType(index: 0,columnIds: [:])
    }
    
    
    @IBAction func doneAct(_ sender: UIButton) {
        if self.columnIdChoseDic.count == 0 {
            BHToast.toast(text: "请选择需要绑定的栏目") 
            return
        }
        self.delegate?.didGuideViewSelectIndexType(index: 1,columnIds:self.columnIdChoseDic)
    }
    
    
}

extension BHCircleGuideChoseView: UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.columnArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let  width = (UIScreen.width-22)/4.0
        return CGSize(width: width-0.5, height: 46)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 11, bottom: 0, right: 11)
    }
}

extension BHCircleGuideChoseView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHCircleTextItemCollectionViewCell", for: indexPath) as! BHCircleTextItemCollectionViewCell
        let model = self.columnArray[indexPath.row]
        cell.text_label.text = model.name
        let choseId = self.columnIdChoseDic[indexPath.row]
        cell.chose = choseId != nil
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let choseId = self.columnIdChoseDic[indexPath.row]
        let model = self.columnArray[indexPath.row]
        if choseId != nil { // 取消
            self.columnIdChoseDic[indexPath.row] = nil
        }else {
            self.columnIdChoseDic[indexPath.row] = model.columnId
        }
        collectionView.reloadData()
    }
}
